<?php
class LastAddition {
	
	private $datum;
	private $jmeno;
	private $type;
	private $id;
	private $id2;

	function __construct($_datum, $_jmeno, $_type, $_id, $_id2){
		$this->datum = $_datum;
		$this->jmeno = $_jmeno;
		$this->type = $_type;
		$this->id = $_id;
		$this->id2 = $_id2;
	}
	
	function getText(){
		if($this->type == COMM_TYPE_REPORT){
			return "<strong>".$this->jmeno."</strong> napsal <a class='preloader' href=\"".redirect_str(PAGE_REPORT).add_par("id_clanku=".$this->id2).add_par("#comment".$this->id)."\"/>komentář k článku</a>";
		} else if($this->type == COMM_TYPE_THEME_COMMENT){
			return "<strong>".$this->jmeno."</strong> přispěl do <a class='preloader' href=\"".redirect_str(PAGE_FORUM_THEME).add_par("id_tema=".$this->id2).add_par("#comment".$this->id)."\"/>diskuze</a>";
		} else if($this->type == COMM_TYPE_NEW_THEME){
			return "<strong>".$this->jmeno."</strong> založil <a class='preloader' href=\"".redirect_str(PAGE_FORUM_THEME).add_par("id_tema=".$this->id)."\"/>nové téma</a>";
		} else if($this->type == COMM_TYPE_UPDATE){
			return "Nová verze webu <strong>".$this->jmeno."</strong> - <a class='preloader' href=\"".redirect_str(PAGE_UPDATES)."\"/>novinky</a>";
		}
	}
	
	function getTimeInLong(){
		return strtotime($this->datum);
	}
	
	function getTime(){
		return DateFormat::format($this->datum, DF_DDMMYYYYHHMM);
	}
}

class LastAdditions {
	
	private $additions = array();
	private $max_results;
	
	function __construct($max_results){
		$this->max_results = $max_results;
	}
	
	function get($index) {
		return $this->additions[$index];
	}
	
	function add($addition) {
		$count = $this->getCount();
		if($count == 0){
			$this->additions[0] = $addition;
			return true;
		}
		for($i = 0; $i < $count; $i++){
			if($addition->getTimeInLong() > $this->get($i)->getTimeInLong()){
				for($n = $count; $n > $i; $n--) {
					$this->additions[$n] = $this->additions[$n - 1];
				}
				$this->additions[$i] = $addition;
				return true;
			}
		}
		if($count < 5){
			$this->additions[$count] = $addition;
			return true;
		}
		return false;
	}
	
	function getCount() {
		$count = count($this->additions);
		return ($count > $this->max_results)?$this->max_results:$count; 
	}
	
}
?>